//
//  LXStatusToolBarView.swift
//  weibo
//
//  Created by lixin on 16/4/22.
//  Copyright © 2016年 lixin. All rights reserved.
//
/*
 
 - 底部tool 显示 转发 评论 赞 的业务逻辑
 - 如果我们的 count <= 0  直接显示  转发 评论 赞 文字
 -例如 count == 0  显示 转发 评论 赞
 - 如果我们的 count > 0 && count < 10000  直接显示 count
 -例如 count == 888  显示 888
 - 如果我们的 count >= 10000  显示 1万 2万
 -例如 count 10000  显示 1万   20000  2万
 -例如 count 12000  显示 1.2万
 
 */

import UIKit

class LXStatusToolBarView: UIView {
    
    var retweetBtn: UIButton?
    
    var commentBtn: UIButton?
    
    var supportBtn: UIButton?
    
    var status: LXStatus?{
        
        didSet{
            
            let retweetResult = getCountString(status?.reposts_count ?? 0, tilte: "转发")
            
            let commentResult = getCountString(status?.comments_count ?? 0, tilte: "评论")
            
            let supportResult = getCountString(status?.attitudes_count ?? 0, tilte: "赞")
            
            retweetBtn?.setTitle(retweetResult, forState: UIControlState.Normal)
            
            commentBtn?.setTitle(commentResult, forState: UIControlState.Normal)
            
            supportBtn?.setTitle(supportResult, forState: UIControlState.Normal)
        
        }
    }
    
    func getCountString(count: Int, tilte: String) -> String{
        
        if count <= 0{
            
            return tilte
            
        }else if count > 0 && count < 10000{
            
            return "\(count)"
            
        }else{
            
            let num = CGFloat(count / 1000) / 10
           
            var countString = "\(num)万"
            
            if countString.containsString(".0"){
                
                countString = countString.stringByReplacingOccurrencesOfString(".0万", withString: "万")//判断是否有0
            }
            
            return countString
            
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setUI(){
        
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
       
        retweetBtn = addButton("timeline_icon_retweet", title: "转发")
        
        commentBtn =  addButton("timeline_icon_comment", title: "评论")
        
        supportBtn =  addButton("timeline_icon_unlike", title: "赞")
        
        //竖线
        let line1 = addLineImageView()
        
        let line2 = addLineImageView()
        
        //MARK: - 添加约束
        retweetBtn!.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.leading.equalTo(self)
            
            make.width.equalTo(commentBtn!)
        }
        
        commentBtn!.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            
            make.leading.equalTo(retweetBtn!.snp_trailing)
            
            make.width.equalTo(supportBtn!)
        }
       
        supportBtn!.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.trailing.equalTo(self)
            
            make.leading.equalTo(commentBtn!.snp_trailing)
        }
        
        line1.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(retweetBtn!.snp_trailing)
            
            make.centerY.equalTo(retweetBtn!)
        }
       
        line2.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(commentBtn!.snp_trailing)
            
            make.centerY.equalTo(commentBtn!)
        }
 
    }
    //MARK: - 创建button
    func addButton(imgName: String, title: String) -> UIButton{
        
        let button = UIButton()
        
        button.setTitle(title, forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        
        addSubview(button)
        
        return button
    }
    
    //MARK: - 创建竖线
    func addLineImageView() -> UIImageView{
        
        let img = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        
        addSubview(img)
        
        return img
    }
    
    
    
    
    
}

